package com.sj.controller;


import com.sj.common.lang.ApiResultMsg;
import com.sj.common.lang.ResultInfo;
import com.sj.entity.Menu;
import com.sj.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Api(tags = "菜单模块")
@RestController
@RequestMapping("/api/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Value("${sjblog.key.menuListKey}")
    private String menuListKey;

    @DeleteMapping("/delete/{menuId}")
    @ApiOperation(value = "删除菜单接口")
    public ApiResultMsg<Menu> delete(@PathVariable("menuId") @ApiParam(name = "menuId", value = "菜单id") Integer menuId) {
        boolean result = menuService.removeById(menuId);
        if (result) {
            redisUtil.del(menuListKey);
            return ApiResultMsg.success(ResultInfo.SUCCESS);
        }
        return ApiResultMsg.error(ResultInfo.ERROR);
    }

    @PutMapping("/save")
    @ApiOperation(value = "添加更新菜单接口")
    public ApiResultMsg<Menu> save(@RequestBody @Validated Menu menu) {
        boolean result = menuService.saveOrUpdate(menu);
        if (result) {
            redisUtil.del(menuListKey);
            return ApiResultMsg.success(ResultInfo.SUCCESS);
        }
        return ApiResultMsg.error(ResultInfo.ERROR);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取所有菜单接口")
    public ApiResultMsg<Menu> listMenu() {
        List<Menu> menuList = menuService.getAllMenuList(0);

        return ApiResultMsg.success(ResultInfo.SUCCESS, menuList);
    }
}

